var mmarkArray = [];

function ShowMarkerInfo(data) {
	
	if(CheckNull(data) || data=="" || data.length==0 )
	{
		DeleteAllMarker();
		return ;
	}
	SetAllMarkerFlase();
	//更新内容
	for (var i = data.length - 1; i >= 0; i--) {
		var knapsackInfoDetail = data[i].knapsack_Detail;
		var distance = data[i].distance;
		var result = UpdateContent(knapsackInfoDetail.id, knapsackInfoDetail.lon,
			knapsackInfoDetail.lat, distance, knapsackInfoDetail.knapsack.type);
		//若存在则直接更新
		if (result) {
			data.splice(i, 1);
		}
	}

	for (var i = mmarkArray.length - 1; i >= 0; i--) {
		var markObject = mmarkArray[i];
		if (markObject.isUser == false) {
			markObject.setMap(null);
			markObject = null;
			mmarkArray.splice(i, 1);
		}
	}

	if (data.length > 0) {
		for (var i = 0; i < data.length; i++) {

			var knapsackInfoDetail = data[i].knapsack_Detail;
			var _disntance = _knapsackDetailList[i].distance;
			var _type = knapsackInfoDetail.knapsack.type;
			var lng = knapsackInfoDetail.lon;
			var lat = knapsackInfoDetail.lat
			var id = knapsackInfoDetail.id;
			var inforId = knapsackInfoDetail.knapsack_info_id;
			var _name = knapsackInfoDetail.knapsack.name;
			var addCount = knapsackInfoDetail.addCountValue;
			
			let markerContent;
			//玩家端
			if(_disntance>0)
			{
				_disntance = _disntance + "M ";
				markerContent =
					`<div style="hCenter">
							<div  class="amap-marker-knapsack">${_disntance}</div>
							<img src="image/knapsack_${_type}.png" style="width:30px;height:30px;pointer-events: none;"> 
						   </div>`;
			}
			//管理端根据距离<=0,
			else
			{
				markerContent =
					`<div style="hCenter">
							<img src="image/knapsack_${_type}.png" style="width:30px;height:30px;pointer-events: none;"> 
						   </div>`;
			}
			var _markObject = new AMap.Marker({
				map: map,
				content: markerContent,
				title: _name,
				position: [lng, lat],
				draggable: false,
				clickable: true,
			});
			_markObject.id = id;
			_markObject.lng = lng;
			_markObject.lat = lat;
			_markObject.inforId = inforId;
			_markObject.iconType = _type;
			_markObject.addCountValue = addCount;

			_markObject.on('click', function(e) {
				let _type = e.target.iconType;
				let addCount = e.target.addCountValue;
				var result = "";
				if (_type == 0) {
					result = "增加命值数量：" + addCount;
				} else if (_type == 1) {
					result = "增加弹量：" + addCount;
				} else if (_type == 2) {
					result += "无敌" + addCount + "分钟";
				} else if (_type == 3) {
					result += "获取金币" + addCount + "枚";
				}

				//mui.alert("背包资料："+GetTips(_type,addCount));
				
			})


			map.add(_markObject);
			mmarkArray.push(_markObject);

		}
	}
}

function GetTips(_type, addCount) {
	var result = "";
	if (_type == 0) {
		result = "增加命值数量：" + addCount;
	} else if (_type == 1) {
		result = "增加弹量：" + addCount;
	} else if (_type == 2) {
		result += "无敌" + addCount + "分钟";
	} else if (_type == 3) {
		result += "获取金币" + addCount + "枚";
	}
	
	return result;
}


///存在则更新内容
function UpdateContent(id, lng, lat, _distance, _type) {
	var result = false;
	for (var i = 0; i < mmarkArray.length; i++) {
		var markObject = mmarkArray[i];
		if (markObject.id == id) {
			if(_distance>0)
			{
				if (markObject._distance != _distance) {
					_distance = _distance + "M ";
					var markerContent =
						`<div style="hCenter">
					 			<div  class="amap-marker-knapsack">${_distance}</div>
					 			<img src="image/knapsack_${_type}.png" style="width:30px;height:30px;pointer-events: none;"> 
					 		   </div>`;
					markObject.setContent(markerContent);
					markObject.isUser = true;
					return true;
				}
			}
			else{
				var markerContent =
					`<div style="hCenter">
				 			<img src="image/knapsack_${_type}.png" style="width:30px;height:30px;pointer-events: none;"> 
				 		   </div>`;
				markObject.setContent(markerContent);
				markObject.isUser = true;
				return true
			}
		}
	}
	return result;
}

function SetAllMarkerFlase() {
	for (var i = 0; i < mmarkArray.length; i++) {
		var markObject = mmarkArray[i];
		markObject.isUser = false;
	}
}

///删除获取的的物资
function DeleteConfirmMarker(confirmList) {
	for (var i = 0; i < confirmList.length; i++) {
		var knapsackId = confirmList[i].id;

		for (var k = mmarkArray.length - 1; k >= 0; k--) {
			var markObject = mmarkArray[k];
			if (markObject.id == knapsackId) {
				markObject.setMap(null);
				markObject = null;
				//移除元素
				mmarkArray.splice(k, 1);
				break;
			}
		}
	}
}

function DeleteAllKnapsackMarker(data)
{
	for (var i = 0; i < mmarkArray.length; i++) {
		var markObject = mmarkArray[i];
		markObject.setMap(null);
		markObject = null;
	}
	
	mmarkArray = [];
}
